package com.qf.rbac2202.sys.controller;

import com.qf.rbac2202.sys.entity.SysMenu;
import com.qf.rbac2202.sys.entity.SysUser;
import com.qf.rbac2202.sys.service.ISysMenuService;
import com.qf.rbac2202.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author dong_
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    ISysMenuService sysMenuService;


    @GetMapping("/nav")
    public R nav(){

        final SysUser currentUser = getCurrentUser();

        List<SysMenu> menuList  = sysMenuService.getUserMenuList(currentUser.getUserId());

        return R.ok().put("menuList", menuList);

    }

    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public List<SysMenu> list(){

        final List<SysMenu> list = sysMenuService.list();
        // 为菜单数据 添加 parentName
        // List<SysMenu> --->  HashMap<菜单id，菜单名字>
        final HashMap<Long, String> map = new HashMap<>();

        list.forEach(sysMenu -> map.put(sysMenu.getMenuId(),sysMenu.getName()));

        list.forEach(sysMenu -> sysMenu.setParentName(map.get(sysMenu.getParentId())));

        return list;

    }

}
